package com.lin.eat.controller;

import com.lin.eat.entry.Address;
import com.lin.eat.entry.Customer;
import com.lin.eat.entry.ServerResponse;
import com.lin.eat.serivce.CustomerService;
import com.lin.eat.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 顾客注册
     * @param customer
     * @return
     */
    @PostMapping("/customerRegister")
    public ServerResponse<Customer> customerRegister(@RequestBody Customer customer){
        return customerService.customerRegister(UuidUtil.createUUID(), customer.getC_name(), customer.getC_phone(), customer.getC_password());
    }

    /**
     * 顾客登录
     * @param customer
     * @return
     */
    @PostMapping("/customerLogin")
    public ServerResponse<Customer> customerLogin(@RequestBody Customer customer){
        return customerService.customerLogin(customer.getC_phone(), customer.getC_password());
    }

    /**
     * 顾客退出登录
     * @param request
     * @return
     */
    @GetMapping("/customerLogout")
    public ServerResponse<Customer> customerLogout(HttpServletRequest request){
        String token = request.getHeader("token");
        return customerService.customerLogout(token);
    }

    /**
     * 获取顾客信息
     * @param request
     * @return
     */
    @GetMapping("/getCustomerInfo")
    public ServerResponse<Customer> getCustomerInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        return customerService.getCustomerInfo(token);
    }

    /**
     * 顾客上传头像
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadCustomerHeadImage")
    public ServerResponse<Customer> uploadHead(MultipartFile file, HttpServletRequest request) throws IOException {
        String token = request.getHeader("token");
        return customerService.updateHeadImage(file, token);
    }

    /**
     * 更新用户名和性别
     * @param customer
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/updateCustomerInfo")
    public ServerResponse<Customer> updateCustomerInfo(@RequestBody Customer customer, HttpServletRequest request){
        String token = request.getHeader("token");
        return customerService.updateCustomerInfo(customer.getC_name(), customer.getC_sex(), token);
    }

    /**
     * 添加收货地址
     * @param address
     * @param request
     * @return
     */
    @PostMapping("/addAddress")
    public ServerResponse addAddress(@RequestBody Address address, HttpServletRequest request){
        String token = request.getHeader("token");
        return customerService.addAddress(address, token);
    }

    /**
     * 获取全部收货地址
     * @param request
     * @return
     */
    @GetMapping("/getAllAddress")
    public ServerResponse<List<Address>> getAllAddress(HttpServletRequest request){
        String token = request.getHeader("token");
        return customerService.getAllAddress(token);
    }

    /**
     * 修改收货地址信息
     * @param address
     * @return
     */
    @PostMapping("/updateAddressInfo")
    public ServerResponse updateAddressInfo(@RequestBody Address address){
        return customerService.updateAddressInfo(address);
    }

    /**
     * 设置为默认地址
     * @param ad_id
     * @return
     */
    @GetMapping("/updateAddressState/{ad_id}")
    public ServerResponse updateAddressState(@PathVariable("ad_id") String ad_id){
        return customerService.updateAddressState(ad_id);
    }

    /**
     * 删除地址
     * @param ad_id
     * @return
     */
    @GetMapping("/deleteAddress/{ad_id}")
    public ServerResponse deleteAddress(@PathVariable("ad_id") String ad_id){
        return customerService.deleteAddress(ad_id);
    }

}
